/**
 * @fileOverview    gulp任务.
 * @author          shaozhe.ysz@alibabapictures.com
 * @since           2016.07.27
 */

'use srtict';

const
    server = require('./picom-core/server'),
    del = require('del'),
    map = require('map-stream'),
    gulp = require('gulp'),
    Cache = require('./picom-core/main/cache'),
    sass = require('gulp-sass'),
    less = require('gulp-less'),
    uglify = require('gulp-uglify'),
    babel = require('gulp-babel');

const
    libPath = ['src/lib/**'],
    jsPath = ['src/!(lib)/**/*.js','src/*.js'],
    sassPath = ['src/**/*.scss'],
    lessPath = ['src/**/*.less'],
    // copyPath = ['src/**/*.css','src/**/*.html'],
    ignore = [];

const config = { base: 'src'};

var handle = function(type){
    return function(event){
        new Process(event)[type]();
    }
}

var Process  = function(event={}){
    this.path = event.path&&event.path.replace(__dirname + '/','')||'';
}

const log = function(msg){
    return map(function(file,cb){
        console.log(msg);
            cb(null,file);
        })
}

Process.prototype = {
    onLibChanged:function(){
        gulp.src(this.path,config)
            .pipe(gulp.dest('build'))
            .pipe(log(`文件:${this.path} | 迁移完成!`));
    },

    onEs6Error:function(err){
        let Default = `${this.path} | babel编译错误!\n`;
        console.log((Default + err.codeFrame)||Default);
    },

    onEs6Changed:function(){
        gulp.src(this.path,config)
            .pipe(babel({presets: ['es2015'] })
                .on('error',this.onEs6Error)
                )
            .pipe(uglify())
            .pipe(gulp.dest('build'))
            .pipe(log(`文件:${this.path} | babel->uglify 编译完成!`));
    },

    onSassChanged:function(){
        gulp.src(this.path,config)
            .pipe(sass.sync().on('error', sass.logError))
            .pipe(gulp.dest('build'))
            .pipe(log(`文件:${this.path} | scss 编译完成!`));
    },



    onLessChanged:function(){
        gulp.src(this.path,config)
            .pipe(less())
            .pipe(gulp.dest('build'))
            .pipe(log(`文件:${this.path} | less 编译完成!`));
    }
}

gulp.task('build',['clear'], function() {
'use srtict';

    gulp.src(libPath,config)
        .pipe(map(function(file,cb){
            var p = new Process();
            p.path = file.path;
            p.onLibChanged();
        }))

    gulp.src(jsPath,config)
        .pipe(map(function(file,cb){
            var p = new Process();
            p.path = file.path;
            p.onEs6Changed();
        }))

    gulp.src(sassPath,config)
        .pipe(map(function(file,cb){
            var p = new Process();
            p.path = file.path;
            p.onSassChanged();
        }))

    gulp.src(lessPath,config)
        .pipe(map(function(file,cb){
            var p = new Process();
            p.path = file.path;
            p.onLessChanged();
        }))
    
});

gulp.task('watch',['build'],function(){
'use srtict';
    server.start();

    gulp.watch(libPath).on('change',handle('onLibChanged'));
    gulp.watch(jsPath).on('change',handle('onEs6Changed'));
    gulp.watch(sassPath).on('change',handle('onSassChanged'));
    gulp.watch(lessPath).on('change',handle('onLessChanged'));
})

gulp.task('clear', function(cb) {
'use srtict';
    Cache.clear()
        .then(()=>console.log('clear success!'))
        .catch(err=>console.log(err));

    del(['build/*','build/**','build/**/*'],cb);
});